package com.jeesite.modules.dataScreen.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.dao.IndustryIndexDao;
import com.jeesite.modules.dataScreen.dao.TrafficZfDao;
import com.jeesite.modules.dataScreen.web.dto.IndustryIndexParam;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import com.jeesite.modules.dataScreen.web.vo.trafficzf.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 交通执法
 */
@Service
public class TrafficZfService {

    @Resource
    private TrafficZfDao trafficZfDao;

    @Resource
    private IndustryIndexService industryIndexService;


    //交通执法获取最新数据
    public TrafficZfVo getTrafficwf() {
        return trafficZfDao.getTrafficwf();
    }

    public JSONObject getQuality() {
        List<JSONObject> quality = trafficZfDao.getQuality(DateUtil.beginOfYear(DateUtil.date()));
        int num = quality.stream().mapToInt(e -> e.getIntValue("count")).sum();
        JSONObject result = JSONObject.of("total", num);
        result.put("datas", quality);
        return result;
    }

    public IPage<AnjianPageVo> anjianPage(Integer pageNum, Integer pageSize, String type) {
        IPage<AnjianPageVo> page = new Page<>();
        page.setSize(pageSize).setCurrent(pageNum);
        return trafficZfDao.anjianPage(page, type);
    }

    public JSONObject anJianMonth(Integer type) {
        List<JSONObject> jsonObjects = trafficZfDao.anjianMonth(type, DateUtil.beginOfYear(DateUtil.date()));
        int num = jsonObjects.stream().mapToInt(e -> e.getIntValue("count")).sum();
        JSONObject result = JSONObject.of("total", num);
        result.put("datas", jsonObjects);
        return result;
    }

    public List<JSONObject> jthytzzb(String tslb) {
        List<JSONObject> result = trafficZfDao.queryJthytzzb(tslb, DateUtil.format(DateUtil.date(), "yyyy-MM"));
        if (CollUtil.isNotEmpty(result) && result.size() > 6) {
            List<JSONObject> newResult = CollUtil.sub(result, 0, 5);
            List<JSONObject> right = CollUtil.sub(result, 5, result.size());
            int sum = right.stream().mapToInt(e -> e.getIntValue("num")).sum();
            JSONObject newData = new JSONObject();
            newData.put("num", sum);
            newData.put("title", "其他");
            newResult.add(newData);
            return newResult;
        }
        return result;
    }

    public JSONObject anjianNumber() {

        String month = DateUtil.format(DateUtil.date(), "yyyy-MM");
        List<JSONObject> jsonObjects = trafficZfDao.anjianNumber(month);
        List<Object> collect = jsonObjects.stream()
                .map(jsonObject -> {
                    jsonObject.put("time", DateUtil.format(jsonObject.getDate("time"), "MM-dd"));
                    return jsonObject;
                }).collect(Collectors.toList());

        JSONObject result = new JSONObject();
        result.put("data", collect);
        return result;
    }

    /**
     * 重点超限数据
     */
    public List<TrafficPointLine> pointLines() {
        return trafficZfDao.pointLines(DateUtil.beginOfDay(DateUtil.date()));
    }

    public CaseHandlerVo caseHandler() {
        Map<String, IndustryIndexVo> indexVoMap = industryIndexService.getUnRealtime(
                "巡游出租汽车车辆守法率",
                "巡游出租汽车从业人员守法率", "巡游出租汽车单位守法率","当月新增案件数","当月办结案件数","交通违法案件平均办案周期");
        CaseHandlerVo result = new CaseHandlerVo();
        result.setCarRate(indexVoMap.get("巡游出租汽车车辆守法率"));
        result.setPersonRate(indexVoMap.get("巡游出租汽车从业人员守法率"));
        result.setCompanyRate(indexVoMap.get("巡游出租汽车单位守法率"));
        result.setDyxbjjs(indexVoMap.get("当月办结案件数"));
        result.setDyxzajs(indexVoMap.get("当月新增案件数"));
        result.setTrafficjtwfajt(indexVoMap.get("交通违法案件平均办案周期"));

        indexVoMap = industryIndexService.getRealtime("全市实时交通运输行政处罚案件总数", "全市实时交通运输行政处罚案件结案率", "全市实时交通运输行政处罚案件复议率",
                "全市实时交通运输行政处罚案件诉讼率", "全市实时交通运输行政处罚案件非诉执行率");
        result.setUnLawsuitRate(indexVoMap.get("全市实时交通运输行政处罚案件非诉执行率"));
        result.setLawsuitRate(indexVoMap.get("全市实时交通运输行政处罚案件诉讼率"));
        result.setPenalize(indexVoMap.get("全市实时交通运输行政处罚案件总数"));
        result.setPenalizeRate(indexVoMap.get("全市实时交通运输行政处罚案件结案率"));
        result.setReconsiderRate(indexVoMap.get("全市实时交通运输行政处罚案件复议率"));


        return result;
    }

    public CheckWarningVO checkWarning() {
        Map<String, IndustryIndexVo> indexVoMap = industryIndexService.getRealtime(
                "全市实时非现场治超超限100%货车预警数", "全市实时非现场治超百吨货车预警数",
                "全市实时\"货车超限违法行为溯源\"多跨事件办结率",
                "全市实时高速公路入口称重货车超限率", "全市实时重点货运源头单位货车超限率",
                "全市实时普通国省干线公路货车超限率");
        CheckWarningVO result = new CheckWarningVO();
        result.setBfbhccx(indexVoMap.get("全市实时非现场治超超限100%货车预警数"));
        result.setBdhccx(indexVoMap.get("全市实时非现场治超百吨货车预警数"));
        result.setMonySjRate(indexVoMap.get("全市实时\"货车超限违法行为溯源\"多跨事件办结率"));
        result.setGaosulkcxRate(indexVoMap.get("全市实时高速公路入口称重货车超限率"));
        result.setYtdwhccxRate(indexVoMap.get("全市实时重点货运源头单位货车超限率"));
        result.setGsgxSjRate(indexVoMap.get("全市实时普通国省干线公路货车超限率"));

        Map<String, IndustryIndexVo> industryIndexVoMap =
                industryIndexService.getUnRealtime("全市实时非现场治超站点设备接入率"
                ,"固定站超限率","全市实时非现场治超站点设备接入率", "全市实时重点货运源头单位货车超限数",
                        "非现场超限率");
        result.setFxccxRate(industryIndexVoMap.get("非现场超限率"));
        result.setGdcxRate(industryIndexVoMap.get("固定站超限率"));
        result.setFxczcSjRate(industryIndexVoMap.get("全市实时非现场治超站点设备接入率"));
        result.setYtdwhccx(industryIndexVoMap.get("全市实时重点货运源头单位货车超限数"));
        return result;
    }
}


